package fctuc.dei.myTwitter.client;

import static fctuc.dei.myTwitter.constants.MyTwitterConstants.TWEET_MAX_LENGTH;


import java.util.Scanner;
import java.util.concurrent.Semaphore;
//import static fctuc.dei.myTwitter.constants.MyTwitterConstants.sleep;
import fctuc.dei.myTwitter.com.MT;
import fctuc.dei.myTwitter.pojo.Tweet;
import fctuc.dei.myTwitter.pojo.User;

public class CommandLine extends Thread{

	Scanner sc;
	private ServerConnection client;
	private Semaphore sem;
	
	public CommandLine(ServerConnection c, Semaphore sem){
		super();
		this.client = c;
		this.sem = sem;
		sc = new Scanner(System.in);
	}
	
	private int validateInt(String input){
		int parsed = 0;
		if(input.length() == 0)
			return parsed;
		try{
			parsed = Integer.parseInt(input);
		} catch (Exception e){
			write("Wrong input...\n");
		}
		return parsed;
	}
	public void run(){
		while(true){
			int op;
			do{
			write("\n" +
					"1 - SEND TWEET\n" +
					"2 - LIST TWEETS\n" +
					"3 - LIST FOLLOWERS\n" +
					"4 - LIST FOLLOWING\n" +
					"5 - SEARCH USER\n" +
					"6 - FOLLOW USER\n" +
					"7 - TOGGLE DISPLAY MODE\n" +
					"8 - LOGOUT\n" +
			"["+ this.client.client.currentUser.getUsername() + " - "+ this.client.client.tech +" ] >>> ");
			}while((op = validateInt(sc.nextLine()))==0);
			
			switch(op){
			case 1:
				String msg = showSendTweet();
				if (msg.length() > 0) {
					Tweet t = new Tweet(msg);
					t.setUser_id(this.client.client.currentUser.getId());
					client.send(MT.TWEET, "TWEET", t);
				}
				break;
			case 2:
				client.send(MT.LIST_TWEETS);
				break;
			case 3:
				client.send(MT.FOLLOWERS);
				break;
			case 4:
				client.send(MT.FOLLOWING);
				break;
			case 5:
				String params = this.showSearchUser();
				if(params.length()>0)
					client.send(MT.SEARCH, "PARAMS", params);
				break;
			case 6:
				String user = this.showFollowUser();
				if(user.length()>0)
					client.send(MT.FOLLOW, "USERNAME", user);
				break;
			case 7:
				if(client.client.displayMode == MT.ASYNC_DISPLAY)
					client.client.displayMode = MT.SYNC_DISPLAY;
				else
					client.client.displayMode = MT.ASYNC_DISPLAY;
				break;
			case 8:
				client.send(MT.LOGOUT, "USER_ID", client.client.currentUser.getId());
//				this.client.client.endSession();
				break;
			default:
				break;
			}
			//fim do switch
			//mutex
		}
	}

	public MT showInit(){
		int op;
		do{
		write("\n1 - Register\n2 - Login\n3 - Quit\n>>> ");
		}while((op = validateInt(sc.nextLine()))==0);

		switch(op){
		case 1:
			return MT.REGISTER;
		case 2:
			return MT.LOGIN;
		case 3:
			return MT.LOGOUT;
		default:
			return MT.UNKNOWN;
		}
	}
	
	public String showSearchUser(){
		write("\nUser to search:");
		return sc.nextLine();
	}

	public String showFollowUser(){
		write("\nUser to follow:");
		return sc.nextLine();
	}
	
	public void showAlternateMenu(){
		write("\n" +
				"1 - SEND TWEET\n" +
				"2 - LIST TWEETS\n" +
				"3 - LIST FOLLOWERS\n" +
				"4 - LIST FOLLOWING\n" +
				"5 - SEARCH USER\n" +
				"6 - FOLLOW USER\n" +
				"7 - LOGOUT\n" +
		"["+ this.client.client.currentUser.getUsername() + " - "+ this.client.client.tech +" ] >>> ");
	}
	
	public User showLogin(){
		User usr = new User();
		write("\n\n   - LOGIN -\n" +
						 "username : ");
		usr.setUsername(sc.nextLine());
		write("password : ");
		usr.setPassword(sc.nextLine());
		return usr;
	}

	public User showRegister(){
		User usr = new User();
		write("\n\n     -  REGISTER USER  -" +
				"<fields signaled with (*) are mandatory>\n" +
						   "username * : ");
		usr.setUsername(sc.nextLine());
		write("password * : ");
		usr.setPassword(sc.nextLine());

		write("email *    : ");
		usr.setEmail(sc.nextLine());

		write("bio        : ");
		usr.setBio(sc.nextLine());

		write("location   : ");
		usr.setCity(sc.nextLine());

		write("url        : ");
		usr.setUrl(sc.nextLine());

		return usr;
	}
	
	public String showSendTweet(){
		boolean inbound = false;
		String tweet = "";
		while(!inbound){
			write("\n[140 char max] - [Empty = cancel]\nYour tweet >>>");
			tweet = sc.nextLine();
			if(tweet.length()<=TWEET_MAX_LENGTH)
				inbound = true;
			else
				write("Too long... You need to short that tweet!\n");
		}
		return tweet;
	}
	
	public synchronized void write(String s){
		try{
			sem.acquire();
		}catch (InterruptedException e){
			e.printStackTrace();}
		if(!this.client.client.benchmarkMode)
			System.out.print(s);
		sem.release();
	}
	
}
